<template>
  <view class="container">
    <!-- 用户信息卡片 -->
    <view class="card">
      <view class="user-info">
        <image class="avatar" :src="userInfo.img" mode="aspectFill"></image>
        <view class="user-details">
          <text class="username">用户名：{{ userInfo.username }}</text>
          <text class="membership">会员等级：{{ userInfo.name }}</text>
          <text class="integral">当前积分：{{ userInfo.integral }}</text>
        </view>
      </view>
    </view>

    <!-- 完成订单将增加相应会员积分提示 -->
    <view class="points-notice">
      <text>完成订单将增加相应会员积分</text>
    </view>

    <!-- 会员等级表格 -->
    <view class="membership-table">
      <view class="table-header">
        <text class="table-cell">会员等级</text>
        <text class="table-cell">积分要求</text>
        <text class="table-cell2">描述</text>
      </view>
      <view v-for="item in membershipList" :key="item.id" class="table-row">
        <text class="table-cell">{{ item.name }}</text>
        <text class="table-cell">{{ item.integral }}</text>
        <text class="table-cell2">{{ item.description }}</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {},
      membershipList: []
    };
  },
  onLoad() {
    this.getUserInfo();
    this.getMembershipList();
  },
  methods: {
    getUserInfo() {
      const token = uni.getStorageSync('Authorization');
      const header = {
        'Authorization': token,
        'Content-Type': 'application/json',
      };
      uni.request({
        url: 'http://localhost:8887/user/userInfo',
        method: 'GET',
        header: header,
        success: (res) => {
          if (res.statusCode === 200 && res.data.success) {
            this.userInfo = res.data.data;
          } else {
            console.error('获取用户信息失败', res);
          }
        },
        fail: (error) => {
          console.error('请求失败', error);
        }
      });
    },
    getMembershipList() {
      uni.request({
        url: 'http://localhost:8887/Membershiptype/findAll',
        method: 'GET',
        success: (res) => {
          if (res.statusCode === 200 && res.data.success) {
            this.membershipList = res.data.data;
          } else {
            console.error('获取会员等级信息失败', res);
          }
        },
        fail: (error) => {
          console.error('请求失败', error);
        }
      });
    }
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
}

.card {
  background-color: #fff;
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.user-info {
  display: flex;
  align-items: center;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin-right: 20px;
}

.user-details {
  display: flex;
  flex-direction: column;
}

.username,
.membership,
.integral {
  margin: 5px 0;
  padding-left: 10px;
}

.points-notice {
  margin-bottom: 20px;
  font-size: 16px;
  color: #333;
  text-align: center;
}

.membership-table {
  background-color: #fff;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.table-header,
.table-row {
  display: table;
  width: 100%;
}

.table-header {
  font-weight: bold;
  border-bottom: 2px solid #eee;
}

.table-cell {
  display: table-cell;
  padding: 10px;
  text-align: left;
  width: 30%;
}

.table-cell2 {
  display: table-cell;
  padding: 10px;
  text-align: left;
  width: 40%;
}

.table-row {
  border-bottom: 1px solid #eee;
}
</style>